﻿Imports DevExpress.XtraGrid.Views.Base

Public Class MemTestForm
    Private WithEvents _aMetaDataTable As CommonExt.MetaDataTable
    Private WithEvents _aMetaDataTableChild As CommonExt.MetaDataTable
    Private WithEvents _aDataset As New DataSet("GridExtPL")
    Private WithEvents _aview As Views.Grid.GridView

    Private _aQueryName As String
    Private _secureGrid As Boolean = False

    Public Property QueryName() As String
        Get
            Return _aQueryName
        End Get
        Set(ByVal value As String)
            _aQueryName = value
        End Set
    End Property

    Public Property DataSet() As DataSet
        Get
            Return _aDataset
        End Get
        Set(ByVal value As DataSet)
            _aDataset = value
        End Set
    End Property

    Public Property SecureGrid() As Boolean
        Get
            Return _secureGrid
        End Get
        Set(ByVal value As Boolean)
            _secureGrid = value
        End Set
    End Property

    Private Sub MemTestForm_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        'If _aMetaDataTable IsNot Nothing Then _aMetaDataTable.Dispose()
        'If _aDataset IsNot Nothing Then _aDataset.Dispose()
        'If _aview IsNot Nothing Then _aview.Dispose()
        'If _aMetaDataTableChild IsNot Nothing Then _aMetaDataTableChild = Nothing
        ''
        '_aMetaDataTable = Nothing
        '_aDataset = Nothing
        '_aview = Nothing
        '_aMetaDataTableChild = Nothing


        'For Each Column As DevExpress.XtraGrid.Columns.GridColumn In CType(Grid.MainView, ColumnView).Columns
        '    If Column.ColumnEdit IsNot Nothing Then
        '        Column.ColumnEdit.Dispose()
        '    End If
        'Next

        Me.Grid.Dispose()
        Me.GridView1.Dispose()
        Me.Dispose()
    End Sub

    Private Sub MemTestForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        InitGridExt()
    End Sub

    Public Function InitData(Optional ByVal withChild As Boolean = True) As Boolean
        'Dim metaDataTableChild As CommonExt.MetaDataTable
        Dim aChildRelationField As String
        Dim parentGuid As Guid = Guid.Empty
        Try
            'Ici on fait nos New a cause des Reload dans la grille.
            Me.DataSet = New DataSet()
            _aMetaDataTable = New MetaDataTable()
            _aMetaDataTable.QueryName = Me.QueryName
            '
            _aMetaDataTable.FillBusinessApplicationData()
            Me.DataSet.Tables.Add(_aMetaDataTable)
            If withChild Then
                Dim aParentRelationField As String = _aMetaDataTable.MetaCommand.GUIDField
                For Each row As DataRow In _aMetaDataTable.MetaCommand.GetChildDisplayQuery.Rows
                    _aMetaDataTableChild = New CommonExt.MetaDataTable() With {.QueryName = row("QueryName").ToString}
                    aChildRelationField = _aMetaDataTable.MetaCommand.GUIDField & "_FK"
                    _aMetaDataTableChild.FillBusinessApplicationData()
                    Me.DataSet.Tables.Add(_aMetaDataTableChild)
                    Dim aDR As New DataRelation(row("Relation").ToString, _aMetaDataTable.Columns(aParentRelationField), _aMetaDataTableChild.Columns(aChildRelationField))
                    Me.DataSet.Relations.Add(aDR)
                Next
            End If
            Return True
        Catch ex As Exception
            Throw ex
        End Try
        Return False
    End Function


    Public Sub InitGridExt(Optional ByVal withChild As Boolean = True)
        'When you define "Event InitDataExt" VB.NET automatically adds a field call
        'InitDataExtEvent. The "InitDataExtEvent" will not show up in the IDE or Intellisense,
        'however you are free to use it in your code
        '
        InitData(False)
        '
        Dim colVisible As Boolean
        Dim aView As GridView = New GridView(Grid)
        Dim Column As DevExpress.XtraGrid.Columns.GridColumn
        '
        '_aDataset.Tables.Add(_aMetaDataTable)
        'Ici on se sert du parent de notre dataset
        'Par convention la table 0 de notre DataSet est le parent de nos relation
        Me.QueryName = _aDataset.Tables(0).TableName
        _aMetaDataTable = CType(_aDataset.Tables(0), MetaDataTable)
        Grid.DataSource = _aDataset.Tables(0)

        'Pour chaque table du DataSet...
        For Each table As MetaDataTable In _aDataset.Tables
            'On regarde les relations de la table...(Les Enfants)
            For Each relation As DataRelation In table.ChildRelations
                aView = New GridView(Grid)
                Grid.LevelTree.Nodes.Add(relation.RelationName, aView)

                'relation.ChildTable est un MetaDataTable, on construit nos DataSet avec des MetaDataTable.
                aView.PopulateColumns(relation.ChildTable)
                '
                'Ici on CTYPE relation.ChildTable car un DataRelation (un type du Framework de MS) contient des DataTable, 
                'comme notre MetaDataTable hérite d'un DataTable il est compatible

                Dim tableColumns As DataTable
                If SecureGrid Then
                    tableColumns = CommonExt.Base.AppManager.GetUserColumnSecurity(CType(relation.ChildTable, MetaDataTable).TableName, CommonExt.Base.AppManager.UserID)
                Else
                    tableColumns = CType(relation.ChildTable, MetaDataTable).MetaData
                End If
                '
                For Each arow As DataRow In tableColumns.Rows
                    colVisible = CBool(IIf(CInt(arow("pVisible")) = 0, False, True))
                    Column = aView.Columns(arow("ColumnName").ToString)
                    Column.VisibleIndex = CInt(arow("pOrder"))
                    Column.Caption = arow("pCaption").ToString
                    Column.Visible = colVisible
                    Column.Width = CInt(arow("pWidth"))
                    Column.OptionsColumn.AllowEdit = Not CBool(arow("pLocked"))
                Next
                aView.HorzScrollVisibility = ScrollVisibility.Always
                aView.OptionsDetail.EnableDetailToolTip = True
                aView.OptionsFilter.DefaultFilterEditorView = FilterEditorViewMode.VisualAndText
                aView.OptionsFilter.ShowAllTableValuesInFilterPopup = True
                aView.OptionsFilter.UseNewCustomFilterDialog = True
                aView.OptionsView.ColumnAutoWidth = False
                aView.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Top
                aView.OptionsView.ShowAutoFilterRow = True
                aView.OptionsView.ShowChildrenInGroupPanel = True
                aView.OptionsSelection.MultiSelect = True
                aView.OptionsView.ShowFilterPanelMode = DevExpress.XtraGrid.Views.Base.ShowFilterPanelMode.ShowAlways
                'Make sure that the columns are define in the view before assignning LOV'S
                'AssignLOV(aView, CType(relation.ChildTable, MetaDataTable))
            Next
        Next
        InitMainView()
    End Sub

    ''' <summary>
    ''' Initialise la grille parents
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub InitMainView()
        Dim colVisible As Boolean
        Dim View As Views.Base.ColumnView = CType(Grid.MainView, ColumnView)
        Dim Column As DevExpress.XtraGrid.Columns.GridColumn

        Dim tableColumns As DataTable

        View.Columns.Clear()

        If SecureGrid Then
            tableColumns = CommonExt.Base.AppManager.GetUserColumnSecurity(_aMetaDataTable.TableName, CommonExt.Base.AppManager.UserID)
        Else
            tableColumns = _aMetaDataTable.MetaData
        End If

        For Each row As DataRow In tableColumns.Rows
            colVisible = CBool(IIf(CInt(row("pVisible")) = 0, False, True))
            Column = View.Columns.AddField(row("ColumnName").ToString)
            Column.VisibleIndex = CInt(row("pOrder"))
            Column.Caption = row("pCaption").ToString
            Column.Visible = colVisible
            Column.Width = CInt(row("pWidth"))
            Column.OptionsColumn.AllowEdit = Not CBool(row("pLocked"))
        Next

        GridView1.GridControl = Me.Grid
        GridView1.HorzScrollVisibility = DevExpress.XtraGrid.Views.Base.ScrollVisibility.Always
        GridView1.Name = "GridView1"
        GridView1.OptionsDetail.AllowExpandEmptyDetails = True
        GridView1.OptionsDetail.EnableDetailToolTip = True
        GridView1.OptionsFilter.DefaultFilterEditorView = DevExpress.XtraEditors.FilterEditorViewMode.VisualAndText
        GridView1.OptionsFilter.ShowAllTableValuesInFilterPopup = True
        GridView1.OptionsFilter.UseNewCustomFilterDialog = True
        GridView1.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Top
        GridView1.OptionsView.ShowAutoFilterRow = True
        GridView1.OptionsView.ShowChildrenInGroupPanel = True
        GridView1.OptionsView.ShowFilterPanelMode = DevExpress.XtraGrid.Views.Base.ShowFilterPanelMode.ShowAlways
        'Make sure that the columns are define in the view before assignning LOV'S
        AssignLOV(CType(View, GridView), _aMetaDataTable)
    End Sub
    Private Sub AssignLOV(ByVal aView As GridView, ByVal aMetaDataTable As MetaDataTable)
        Dim aMetaquery As MetaQuery
        Dim colVisible As Boolean
        Dim View As Views.Base.ColumnView '= aLOV.View
        Dim Column As DevExpress.XtraGrid.Columns.GridColumn

        Dim aLOV As RepositoryItemGridLookUpEdit

        For Each lookup As KeyValuePair(Of String, Guid) In aMetaDataTable.ListOfLov
            'testPoint: Verify the GuidField in metaquery , The Guidfield is CASE sensitive, IE: ListvalueID is not equal to ListValueID
            aMetaquery = CommonExt.MetaQuery.GetMetaQueryById(lookup.Value)
            aMetaquery.FillResultQuery()
            'outstandingissue : will have to check the language and get the 2nd or 3thr column of LOV according to LOV Standard 

            aLOV = New RepositoryItemGridLookUpEdit() With {.DataSource = aMetaquery.ResultQuery, .DisplayMember = aMetaquery.ResultQuery.Columns(CommonExt.Base.AppManager.ApplicationOption.DescriptionColumnIndex).ToString, .ValueMember = aMetaquery.GUIDField, .PopupFormSize = New Size(800, 300), .NullText = "(none)"}
            aLOV.View.HorzScrollVisibility = ScrollVisibility.Always
            aLOV.View.OptionsView.ColumnAutoWidth = False
            aLOV.View.OptionsView.ShowAutoFilterRow = True
            aLOV.View.OptionsBehavior.ReadOnly = True
            View = aLOV.View
            View.Columns.Clear()
            For Each row As DataRow In aMetaquery.MetaColumns.Rows
                colVisible = CBool(IIf(CInt(row("pVisible")) = 0, False, True))
                Column = View.Columns.AddField(row("ColumnName").ToString)
                Column.VisibleIndex = CInt(row("pOrder"))
                Column.Caption = row("pCaption").ToString
                Column.Visible = colVisible
                Column.Width = CInt(row("pWidth"))
            Next
            Me.Grid.RepositoryItems.Add(aLOV)
            aView.Columns(lookup.Key).ColumnEdit = aLOV
            aMetaquery.Dispose()
            'aLOV.Dispose()
        Next
    End Sub
End Class